﻿package tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class TransientDemo implements Serializable {
	private static final long serialVersionUID = 1L;
	private Date loggingDate = new Date();
	private String uid;
	// pwd的值将不会被保存到文件
	private transient String pwd;

	TransientDemo(String user, String password) {
		uid = user;
		pwd = password;
	}

	public String toString() {
		String password = null;
		if (pwd == null) {
			password = "NOT SET";
		} else {
			password = pwd;
		}
		return "logon info: \n   " + "user: " + uid + "\n   logging date : "
				+ loggingDate.toString() + "\n   password: " + password;
	}

	public static void main(String[] args) {

		TransientDemo logInfo = new TransientDemo("MIKE", "MECHANICS");
		System.out.println(logInfo.toString());
		try {
			ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
					"tmpfile\\dat\\login.dat"));
			o.writeObject(logInfo);
			o.close();
		} catch (Exception e) {// deal with exception

		}

		// To read the object back, we can write
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					"tmpfile\\dat\\login.dat"));
			TransientDemo logInfo1 = (TransientDemo) in.readObject();
			System.out.println(logInfo1.toString());
		} catch (Exception e) {// deal with exception

		}
	}
}
